<?php
/**
 * @description : encrypt or decrypt a string.The base operation of cyrptograme
 * @class-name  : BasedCryptogram.
 * @method      : encrypt().
 * @method      : decrypt().
 * @author      : stephen.mo@hotmail.com
 * @version     : $Id , 1.0.0
 * @date        : Mar 24,2011
 */

class BasedCryptogram {
    
    /** constructor of class */
    public function __construct() {/*{{{*/

    }/*}}}*/

    /**
     * @description : openssl encrypt a string.
     * @parameter   : (string)$source - A source string will be encrypt.
     * @parameter   : (string)$key    - A encrypt key will be.
     * @return      : String.
     * @author      : stephen.mo <stephen.mo@hotmail.com>
     * @date        : Mar 24,2011
     */
    static public function encrypt($source,$key) {/*{{{*/
        if (empty($source) || empty($key)) return FALSE;
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
        $encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, trim($source), MCRYPT_MODE_ECB, $iv);
        $result = trim(base64_encode($encrypted_string));
        return str_replace(' ','+',$result);

    }/*}}}*/


    /**
     * @description : openssl decrypt a string.
     * @parameter   : (string)$source - A string for encrypted will be decrypt
     * @parameter   : (string)$key    - A decrypt key will be.
     * @return      : String
     * @author      : stephen.mo <stephen.mo@hotmail.com>
     * @date        : Mar 24,2011
     */
    static public function decrypt($source,$key) {/*{{{*/
        if (empty($source) || empty($key)) return FALSE;
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
        $decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($source), MCRYPT_MODE_ECB, $iv);
        return trim($decrypted_string);

    }/*}}}*/


}

?>

